package com.ihd.ihardware.view.tzc.me.model;

import com.google.gson.JsonObject;
import com.ihd.ihardware.api.HttpClient;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.FileRes;
import com.ihd.ihardware.pojo.HealthRecordRes;
import com.ihd.ihardware.pojo.MembersRes;

import org.reactivestreams.Publisher;

import java.io.File;

import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MemberRepository extends BaseRepository {

    public void addMember(String avatar, int relation, int sex, String birthday, String height, String weight) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("avatar", avatar);
        mParam.addProperty("relation", relation);
        mParam.addProperty("weight", weight);
        mParam.addProperty("sex", sex);
        mParam.addProperty("birthday", birthday);
        mParam.addProperty("height", height);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().addMember(rb)
                .flatMap(new Function<EmptyRes, Publisher<MembersRes>>() {
                    @Override
                    public Publisher<MembersRes> apply(EmptyRes emptyRes) throws Exception {
                        return HttpClient.getService().members();
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<MembersRes>(AppConstans.ADDMEMBER) {
                }));
    }

    public void handWeighing(String familyId, String weight) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("familyId", familyId);
        mParam.addProperty("weight", weight);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().handWeighing(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.HANDWEIGHT) {
                }));
    }


    public void editMember(String avatar, int relation, int sex, String birthday, String height, String weight, String memberId) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("avatar", avatar);
        mParam.addProperty("relation", relation);
        mParam.addProperty("weight", weight);
        mParam.addProperty("sex", sex);
        mParam.addProperty("birthday", birthday);
        mParam.addProperty("height", height);
        mParam.addProperty("memberId", memberId);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().editMember(rb)
                .flatMap(new Function<EmptyRes, Publisher<MembersRes>>() {
                    @Override
                    public Publisher<MembersRes> apply(EmptyRes emptyRes) throws Exception {
                        return HttpClient.getService().members();
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<MembersRes>(AppConstans.ADDMEMBER) {
                }));
    }

    public void updateHead(File file) {
        MultipartBody.Part requestBody = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
        addDisposable(HttpClient.getService().uploadToOss(requestBody)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<FileRes>(AppConstans.UPDATE) {
                }));
    }

    public void delMember(String memberId) {
        addDisposable(HttpClient.getService().delMember(memberId)
                .flatMap(new Function<EmptyRes, Publisher<MembersRes>>() {
                    @Override
                    public Publisher<MembersRes> apply(EmptyRes emptyRes) throws Exception {
                        return HttpClient.getService().members();
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<MembersRes>(AppConstans.DELMEMBER) {
                }));
    }

    public void healthRecord() {
        addDisposable(HttpClient.getService().healthRecord()
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<HealthRecordRes>(AppConstans.HEALTH) {
                }));
    }

    public void healthRecord(String birthday, String exerciseFrequency, String health, String height, String id, String sex, String waist, String want, String weight, String eatingHabits) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("birthday", birthday);
        mParam.addProperty("exerciseFrequency", exerciseFrequency);
        mParam.addProperty("health", health);
        mParam.addProperty("height", height);
        mParam.addProperty("id", id);
        mParam.addProperty("sex", sex);
        mParam.addProperty("waist", waist);
        mParam.addProperty("want", want);
        mParam.addProperty("weight", weight);
        mParam.addProperty("eatingHabits", eatingHabits);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().healthRecord(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.EDITHEALTH) {
                }));
    }
}
